package com.zyx.jdbcdemo.hikari;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author Yaxi.Zhang
 * @since 2021/6/10 16:41
 * desc: Hikari池连接MySQL案例
 */
public class HikariDemo {
    public static void main(String[] args) throws SQLException, InterruptedException {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3306/db_test?useSSL=false");
        config.setUsername("root");
        config.setPassword("123321");
        // 连接超时：1秒
        config.addDataSourceProperty("connectionTimeout", "1000");
        // 空闲超时：60秒
        config.addDataSourceProperty("idleTimeout", "60000");
        // 最大连接数：10
        config.addDataSourceProperty("maximumPoolSize", "10");
        HikariDataSource hikariDataSource = new HikariDataSource(config);
        Connection conn = hikariDataSource.getConnection();
        Thread.sleep(50000L);
        System.out.println(conn);
    }
}
